package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;


    @Override
    public R addCart(ShoppingCart shoppingCart) {

        /*
            张三 --  辣子鸡丁   1份     添加 张三 辣子鸡丁  1份
                              1份     修改             2份
                              1份     修改             3份
                    经济套餐    1份    添加             1份
                              1份     修改            2份
                    酸菜鱼     1份

            1、先判断该用户是否已经点过该菜品或者是套餐
            2、如果点过了,修改原有的数据：份数+1
            3、如果没有点过：新增一条数据到购物车表中
         */
        //1、先判断该用户是否已经点过该菜品或者是套餐
        // select * from shopping_cart where user_id = ? and   dish_id =? / setmeal_id=?

        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, UserHolder.getUserId());
        qw.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        qw.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());

        ShoppingCart shoppingCartDB = shoppingCartMapper.selectOne(qw);
        if(shoppingCartDB != null){
            //2、如果点过了,修改原有的数据：份数+1
            shoppingCartDB.setNumber(shoppingCartDB.getNumber()+1);
            shoppingCartMapper.updateById(shoppingCartDB);
            return R.success(shoppingCartDB);
        }else {
            //  3、如果没有点过：新增一条数据到购物车表中
            //用户id
            shoppingCart.setUserId(UserHolder.getUserId());
            //数量 number 1
            shoppingCart.setNumber(1);
            shoppingCartMapper.insert(shoppingCart);
            return R.success(shoppingCart);
        }
    }

    @Override
    public R subCart(ShoppingCart shoppingCart) {
        /*
            1、查询购物车中该菜品/套餐的数据
            2、对 num进行--操作
            3、更新数据
         */
        // 1、select * from shopping_cart where user_id = ? and   dish_id =? / setmeal_id=?
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, UserHolder.getUserId());
        qw.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        qw.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        ShoppingCart shoppingCartDB = shoppingCartMapper.selectOne(qw);
        if(shoppingCartDB==null){
            return R.error("该商品未在购物车中！");
        }
        shoppingCartDB.setNumber(shoppingCartDB.getNumber()-1);
        if(shoppingCartDB.getNumber()<=0){//将该删除从购物车中移除
            // delete from shopping_cart where id = ?
            shoppingCartMapper.deleteById(shoppingCartDB.getId());
        }else {
            shoppingCartMapper.updateById(shoppingCartDB);
        }
        return R.success(shoppingCartDB);
    }



}
